package com.yyx.lgb.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.yyx.lgb.model.entity.product.Category;
import com.yyx.lgb.product.mapper.CategoryMapper;
import com.yyx.lgb.product.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * ClassName: CategoryServiceImpl
 * Package: com.yyx.lgb.product.service.impl
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/8/11 - 14:15
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public List<Category> findOneCategory() {
        //查询redis是否有一级分类
        String category = redisTemplate.opsForValue().get("category:one");
        //如果redis包含一级分类 返回
        if(StringUtils.hasText(category)){
            //category字符转集合
            List<Category> existCategoryList = JSON.parseArray(category, Category.class);
            log.info("从redis查询到一级分类数据");
            return existCategoryList;
        }
        //如果redis不包含一级分类 查询数据库 放到redis
        List<Category> CategoryList = categoryMapper.findOneCategory();
        redisTemplate.opsForValue().set("category:one",JSON.toJSONString(CategoryList),7, TimeUnit.DAYS);
        return CategoryList;
    }

    @Override
    @Cacheable(value = "category",key = "'all'")
    public List<Category> findCategoryTree() {

        //查询所有分类 返回list
       List<Category> categoryList= categoryMapper.findAll();

       //遍历集合
       List<Category>oneCategoryList= categoryList.stream().filter(item->item.getParentId().longValue()==0).collect(Collectors.toList());
       //判断一级分类不为空 得到一级下面的二级分类
       if(!CollectionUtils.isEmpty(categoryList)){
            //遍历二级id=parent_id 得到下一级分类
           oneCategoryList.forEach(oneCategory->{
               List<Category> twoCategoryList=
                       categoryList.stream().filter(item->item.getParentId().longValue()==
                               oneCategory.getId().longValue()).collect(Collectors.toList());
               //二级分类封装到一级里面
               oneCategory.setChildren(twoCategoryList);
               if(!CollectionUtils.isEmpty(twoCategoryList)){
                   //遍历二级分类 id=parent_id 得到二级下面的三级分类
                   twoCategoryList.forEach(twoCategory->{
                  List<Category> threeCategoryList=categoryList.stream().filter(item->item.getParentId().longValue()
                               ==twoCategory.getId().longValue()).collect(Collectors.toList());
                        twoCategory.setChildren(threeCategoryList);
                   });
               }
           });


        }
        return oneCategoryList;
    }
}
